package com.alexander.cloundexplorer.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;

import com.alexander.cloundexplorer.R;
import com.alexander.cloundexplorer.activity.RenameActivity;
import com.alexander.cloundexplorer.bean.params.TrashFileParams;
import com.alexander.cloundexplorer.bean.params.User;
import com.alexander.cloundexplorer.bean.response.FileInfoResponse;
import com.alexander.cloundexplorer.util.Constants;
import com.alibaba.fastjson.JSON;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 *
 * @author Administrator
 * @date 2018/3/9
 */

public class FileOptionsDialog extends BaseDialog
{
    private FileInfoResponse fileInfoResponse;
    private int layoutId = R.layout.dialog_fileoptions_layout;
    private TrashFileParams trashFileParams = new TrashFileParams();
    private OnDelLisnter onDelLisnter;

    public FileOptionsDialog(@NonNull Context context1, FileInfoResponse fileInfoResponse, User user1)
    {
        super(context1, user1);
        this.fileInfoResponse = fileInfoResponse;
    }

    public FileOptionsDialog(@NonNull Context context1, int themeResId, FileInfoResponse fileInfoResponse, User user1)
    {
        super(context1, themeResId, user1);
        this.fileInfoResponse = fileInfoResponse;
    }

    public FileOptionsDialog(@NonNull Context context1, boolean cancelable, @Nullable OnCancelListener cancelListener, FileInfoResponse fileInfoResponse, User user1)
    {
        super(context1, cancelable, cancelListener, user1);
        this.fileInfoResponse = fileInfoResponse;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(layoutId);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.reName_TextView, R.id.trash_TextView})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.reName_TextView:
                Intent intent = new Intent(getContext(), RenameActivity.class);
                intent.putExtra("isFile", "1");
                intent.putExtra("info", fileInfoResponse);
                intent.putExtra("user", user);
                getContext().startActivity(intent);
                dismiss();
                break;
            case R.id.trash_TextView:
                trashFileParams.setAccount(user.getPhone());
                trashFileParams.setPassword(user.getPassword());
                trashFileParams.setFileId(fileInfoResponse.getId().toString());
                OkHttpUtils
                        .post()
                        .url(Constants.IP.concat("/fileController/trashFile"))
                        .addParams("params", JSON.toJSONString(trashFileParams))
                        .build()
                        .execute(new StringCallback()
                        {
                            @Override
                            public void onError(Call call, Exception e, int id)
                            {
                                if(onDelLisnter != null)
                                {
                                    onDelLisnter.onDelError(e.getCause());
                                }
                            }

                            @Override
                            public void onResponse(String response, int id)
                            {
                                showTip(response);
                                if(onDelLisnter != null)
                                {
                                    if(response.contains("700"))
                                    {
                                        onDelLisnter.onDelSuccess(response, fileInfoResponse);
                                    }
                                    else if(response.contains("701"))
                                    {
                                        Throwable throwable = new Throwable(response);
                                        onDelLisnter.onDelError(throwable);
                                    }
                                }
                            }
                            @Override
                            public void onAfter(int id)
                            {
                                super.onAfter(id);
                                FileOptionsDialog.this.dismiss();
                            }
                        });
                break;
            default:
                break;
        }
    }

    /**
     * 设置列表项点击监听器
     * */
    public void setOnDelLisnter(OnDelLisnter onDelLisnter)
    {
        this.onDelLisnter = onDelLisnter;
    }
    public interface OnDelLisnter
    {
        /**
         * 删除成功的会掉
         * @param result 返回值
         * @param fileInfoResponse 被删除的文件的对象
         * */
        void onDelSuccess(String result, FileInfoResponse fileInfoResponse);
        /**
         * 删除失败的回调
         * @param throwable 错误
         * */
        void onDelError(Throwable throwable);
    }
}
